package com.shengqin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shengqin.common.pojo.CommonResult;
import com.shengqin.config.CommonException;
import com.shengqin.entity.RunLine;
import com.shengqin.service.impl.RunLineService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
public class RunLineController {
    @Resource
    private RunLineService runLineService;

    @GetMapping({"admin/v1/runLine/list","api/v1/runLine/list"})
    public CommonResult<List<RunLine>> queryList(RunLine runLine) {
        QueryWrapper<RunLine> wrapper = new QueryWrapper<>();
        if(runLine.getSchoolId() != null){
            wrapper.lambda().eq(RunLine::getSchoolId, runLine.getSchoolId());
        }
        wrapper.lambda().eq(RunLine::getEnabled, true);
        wrapper.lambda().orderByDesc(RunLine::getCreateTime);
        return CommonResult.data(runLineService.list(wrapper));
    }

    @PostMapping("admin/v1/runLine/save")
    public CommonResult<Boolean> save(@RequestBody RunLine runLine) {
        if(runLine.getId() == null){
            runLine.setCreateTime(LocalDateTime.now());
        }
        return CommonResult.data(runLineService.saveOrUpdate(runLine));
    }

    @PostMapping("admin/v1/runLine/delete")
    public CommonResult<Boolean> delete(@RequestBody RunLine runLine) {
        if(runLine.getId() == null){
            throw new CommonException("请选择需要删除的数据");
        }
        return CommonResult.data(runLineService.removeById(runLine.getId()));
    }

}
